#include <QtCore>
#include <QtGui>

#include "SettingDialog.h"
#include "ui_SettingDialog.h"
#include "mixiVoice.h"

SettingDialog::SettingDialog(QSettings *s, QWidget *parent) :
	QDialog(parent),
	m_ui(new Ui::SettingDialog), settings(s)
{
	m_ui->setupUi(this);
	m_ui->textColor->addItems(QColor::colorNames());
	connect(m_ui->textColor, SIGNAL(currentIndexChanged(int)), this, SLOT(applyColors()));
	m_ui->backgroundColor->addItems(QColor::colorNames());
	connect(m_ui->backgroundColor, SIGNAL(currentIndexChanged(int)), this, SLOT(applyColors()));

	m_ui->hiddenUserID->setValidator(new QIntValidator(this));
	connect(m_ui->addHiddenUser, SIGNAL(clicked()), this, SLOT(addHiddenUser()));
	connect(m_ui->deleteHiddenUser, SIGNAL(clicked()), this, SLOT(removeHiddenUser()));

	connect(m_ui->clearImageCache, SIGNAL(clicked()), this, SLOT(clearImageCache()));

	if (settings == NULL)	return;

	m_ui->email->setText(settings->value(Setting::EMail).toString());
	m_ui->password->setText(settings->value(Setting::Password).toString());
	m_ui->savePassword->setChecked(settings->value(Setting::SavePassword, m_ui->savePassword->isChecked()).toBool());
	m_ui->reloadInterval->setValue(settings->value(Setting::ReloadInterval, m_ui->reloadInterval->value()).toInt());
	m_ui->alwaysOnTop->setChecked(settings->value(Setting::AlwaysOnTop, m_ui->alwaysOnTop->isChecked()).toBool());
	m_ui->notificationShowTime->setValue(settings->value(Setting::NotificationShowTime, m_ui->notificationShowTime->value()).toInt());

	m_ui->maxVoiceCount->setValue(settings->value(Setting::MaxVoiceCount, m_ui->maxVoiceCount->value()).toInt());
	m_ui->voiceFormat->setText(settings->value(Setting::VoiceFormat, m_ui->voiceFormat->text()).toString());
	m_ui->dateTimeFormat->setText(settings->value(Setting::DateTimeFormat, m_ui->dateTimeFormat->text()).toString());
	m_ui->textColor->setCurrentIndex(m_ui->textColor->findText(settings->value(Setting::WindowTextColor, "black").toString()));
	m_ui->backgroundColor->setCurrentIndex(m_ui->textColor->findText(settings->value(Setting::WindowBackgroundColor, "lightgray").toString()));

	m_ui->hiddenUserList->addItems(settings->value(Setting::HiddenUsers).toStringList());
}

SettingDialog::~SettingDialog()
{
	delete m_ui;
}

void SettingDialog::accept()
{
	settings->setValue(Setting::EMail, m_ui->email->text());
	settings->setValue(Setting::Password, m_ui->password->text());
	settings->setValue(Setting::SavePassword, m_ui->savePassword->isChecked());
	settings->setValue(Setting::ReloadInterval, m_ui->reloadInterval->value());
	settings->setValue(Setting::AlwaysOnTop, m_ui->alwaysOnTop->isChecked());
	settings->setValue(Setting::NotificationShowTime, m_ui->notificationShowTime->value());
	settings->setValue(Setting::VoiceFormat, m_ui->voiceFormat->text());
	settings->setValue(Setting::DateTimeFormat, m_ui->dateTimeFormat->text());
	settings->setValue(Setting::MaxVoiceCount, m_ui->maxVoiceCount->value());
	settings->setValue(Setting::WindowTextColor, m_ui->textColor->currentText());
	settings->setValue(Setting::WindowBackgroundColor, m_ui->backgroundColor->currentText());

	QStringList hiddenUsers;
	for (int i = 0; i != m_ui->hiddenUserList->count(); ++i)
		hiddenUsers.append(m_ui->hiddenUserList->item(i)->text());
	settings->setValue(Setting::HiddenUsers, hiddenUsers);

	QDialog::accept();
}

void SettingDialog::applyColors()
{
	m_ui->sample->setText(QString("<p style=\"color:%1; background-color:%2;\">sample text</p>").arg(m_ui->textColor->currentText()).arg(m_ui->backgroundColor->currentText()));
}

void SettingDialog::addHiddenUser()
{
	const QString id = m_ui->hiddenUserID->text();
	if (m_ui->hiddenUserList->findItems(id, Qt::MatchExactly).count() == 0)
		m_ui->hiddenUserList->addItem(id);
}

void SettingDialog::removeHiddenUser()
{
	delete m_ui->hiddenUserList->takeItem(m_ui->hiddenUserList->currentRow());
}

void SettingDialog::clearImageCache()
{
	QDir dir(QDir::tempPath());
	QStringList list = dir.entryList(QStringList() << "mixiVoice_*");
	for (int i = 0; i != list.count(); ++i)
		QFile(QString("%1/%2").arg(dir.absolutePath()).arg(list[i])).remove();
	QMessageBox::information(this, tr("Cache Cleared"), tr("All image cache has been deleted."));
}

void SettingDialog::changeEvent(QEvent *e)
{
	QDialog::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		m_ui->retranslateUi(this);
		break;
	default:
		break;
	}
}
